package com.hxzy.service;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ArithmeticValidationProxy {
    private ArithmeticCalcutor target;

    public ArithmeticValidationProxy(ArithmeticCalcutor target) {
        this.target = target;
    }
    
    public ArithmeticCalcutor proxy(){
        InvocationHandler handler=new InvocationHandler() {
            
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                // TODO Auto-generated method stub
                if(Integer.parseInt(args[0].toString())<=0||Integer.parseInt(args[1].toString())<=0){
                    throw new IllegalArgumentException("參數必須大於0");
                }
                Object result=method.invoke(target, args);
                return result;
            }
        };
        return (ArithmeticCalcutor) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), handler);
    }
}
